package it.valeandre.gollometro.c2dm;

import it.valeandre.gollometro.model.TokenUpdate;
import it.valeandre.gollometro.net.NetworkService;
import it.valeandre.gollometro.net.NetworkServiceListener;
import it.valeandre.gollometro.utils.Utils;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public final class C2DMUtils {
	private C2DMUtils() {

	}

	public static void registerForPushNotification(Context context) {
//		try {
//			SharedPreferences prefs = context.getSharedPreferences(C2DMReceiver.PREFS_C2DM, Context.MODE_PRIVATE);
//
//			String currentRegistrationId = prefs.getString(C2DMReceiver.PREFS_KEY_C2DM_LAST_KNOWN_REGISTRATION_ID, null);
//
//			if (currentRegistrationId == null) {
//				Log.d("TAG", "Gollometro: asking for registration on C2DM...");
//				Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
//				registrationIntent.putExtra("app", PendingIntent.getBroadcast(context, 0, new Intent(), 0));
//				registrationIntent.putExtra("sender", ResBundle.C2DM_REGISTERED_ACCOUNT);
//				context.startService(registrationIntent);
//			} else {
//				checkRegistrationID(context);
//			}  
//		} catch (Exception e) {
//			// nothing to do....
//		}
		
		Log.d("TAG", "performing registerForPushNotification");
		try {
			SharedPreferences prefs = context.getSharedPreferences(C2DMReceiver.PREFS_C2DM, Context.MODE_PRIVATE);

			String currentRegistrationId = prefs.getString(C2DMReceiver.PREFS_KEY_C2DM_LAST_KNOWN_REGISTRATION_ID, null);
			
			if (currentRegistrationId == null) {
				Log.d("TAG", "FIRST TIME REG");
				Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
				registrationIntent.putExtra("app", PendingIntent.getBroadcast(context, 0, new Intent(), 0));
				registrationIntent.putExtra("sender", "valentina.armenise@gmail.com");
//				registrationIntent.putExtra("sender", emailAccount);
				context.startService(registrationIntent);
			} else {
//				checkRegistrationID(context, false);
			}
		} catch (Exception e) {
			// nothing to do....
		}
	}

	public static void checkRegistrationID(Context context) {
		try {
			final SharedPreferences prefs = context.getSharedPreferences(C2DMReceiver.PREFS_C2DM, Context.MODE_PRIVATE);

			String currentRegistrationId = prefs.getString(C2DMReceiver.PREFS_KEY_C2DM_LAST_KNOWN_REGISTRATION_ID, null);
			final String oldRegistrationId = prefs.getString(C2DMReceiver.PREFS_KEY_C2DM_OLD_REGISTRATION_ID, null);

			if (oldRegistrationId != null && currentRegistrationId != null) {

				if (!oldRegistrationId.equals(currentRegistrationId)) {

					Utils.hardLong("Gollometro C2DM: there is an old registration, need to update server-side ");
					
					NetworkService.getInstance().updateToken(new TokenUpdate(oldRegistrationId, currentRegistrationId), new NetworkServiceListener() {
						@Override
						public void tokenUpdatePerformed(int statusCode) {
							Editor editor = prefs.edit();
							editor.remove(C2DMReceiver.PREFS_KEY_C2DM_OLD_REGISTRATION_ID);
							editor.commit();

							Utils.hardLong("Gollometro C2DM: token updated");
						}

						@Override
						public void tokenUpdateFailed(int statusCode) {
							// nothing to do...
						}

						@Override
						public void tokenUpdateFailed(Exception e) {
							// nothing to do...
						}
					});
				} else {
					Utils.hardLong("Gollometro C2DM: unxpected old token == new token");
					
					Editor editor = prefs.edit();
					editor.remove(C2DMReceiver.PREFS_KEY_C2DM_OLD_REGISTRATION_ID);
					editor.commit();
				}
			}

		} catch (Exception e) {
			// notihing to do...
		}
	}
}
